<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="add({pid:'',type:1})">添加权限</el-button>
        </div>
        <el-table :data="list" row-key="id" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="add({pid:row.id,type:2})">添加</el-button>
              <el-button type="text" @click="edit(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog :visible="showDialog" :title="`${!formData.id?'添加':'编辑'}权限`" @close="closeDialog">
          <el-form label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>
          <template #footer>
            <div style="text-align: right;">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="confirm">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>

    </div>
  </div>
</template>

<script>
import { getPermissionListApi, addPermission, delPermissionApi, getPermissionDetail, updatePermission } from '@/api'
import transTree from '@/utils/transTree'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      // 弹框显隐
      showDialog: false,
      // 弹框收集的数据
      formData: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: 1, // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  mounted () {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      try {
        const res = await getPermissionListApi()
        // console.log(res)
        this.list = transTree(res.data)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加按钮点击事件
    add({ pid, type }) {
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    // 关闭弹框
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: 1, // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 弹框确定按钮
    async confirm() {
      if (!this.formData.id) {
        await addPermission(this.formData)
      } else {
        await updatePermission(this.formData)
      }

      this.$message.success(`${!this.formData.id ? '添加' : '编辑'}成功`)
      this.getPermissionList()
      this.closeDialog()
    },
    // 删除权限
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delPermissionApi(id)
        this.$message.success('删除权限成功')
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑权限
    async edit(id) {
      const res = await getPermissionDetail(id)
      this.formData = res.data
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped></style>
